package com.xindaoapp.framework.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.framework.R;
import com.xindaoapp.framework.api.AddClient;
import com.xindaoapp.framework.api.Address;
import com.xindaoapp.framework.api.ConstantsValue;
import com.xindaoapp.framework.base.BaseActivity;
import com.xindaoapp.framework.bean.UserInfo;
import com.xindaoapp.framework.utils.ProgressHUD;

/**
 * ============================================================
 * 
 * 版权 : 新道APP (c) 2014
 * 
 * 作者 : 贺奕凯
 * 
 * 版本 : 1.0
 * 
 * 创建日期 : 2014-6-24 下午4:19:26
 * 
 * 描述 : 账号绑定页面
 * 
 * 修订历史 ：
 * 
 * ============================================================
 **/
public class BindActivity extends BaseActivity implements OnClickListener {
	@ViewInject(R.id.tittle_common_content)
	private TextView tittle_common_content;
	@ViewInject(R.id.tittle_common_register)
	private TextView tittle_common_register;
	@ViewInject(R.id.bind_username)
	private EditText bind_username;
	@ViewInject(R.id.bind_password)
	private EditText bind_password;
	@ViewInject(R.id.bind)
	private Button bind;
	
	protected static final int LOGIN_OK = 0;
	protected static final int LOGIN_TRUE= 10;
	private ImageView tittle_common_cancel;
	private String username;
	private String password;
	private String tittle;

	private final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case LOGIN_OK:
				finish();
				break;
			default:
				break;
			}
		};
	};
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bind);
		ViewUtils.inject(this); // 加载注释控件
		sp = getSharedPreferences("userconfig", MODE_PRIVATE);
		tittle_common_cancel = (ImageView) findViewById(R.id.tittle_common_cancel);
		
		tittle = getIntent().getExtras().getString("tittle");
		tittle_common_content.setText(tittle);

		setListener(); //添加监听事件
	}

	private void setListener() {
		bind_username.setOnClickListener(this);
		bind_password.setOnClickListener(this);
		tittle_common_cancel.setOnClickListener(this);
		tittle_common_register.setOnClickListener(this);
		bind.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tittle_common_cancel:
			finish();
			break;
		case R.id.bind:
			if (tittle.equals("绑定QQ帐号")) {
				username = bind_username.getText().toString().trim();
				password = bind_password.getText().toString().trim();
				if (checkUserName() && checkPassword()) {
					Log.i("login", "username" + username + "password" + password);
					bindQQ(username, password);
				}
			}
			if(tittle.equals("绑定微博帐号")){
				username = bind_username.getText().toString().trim();
				password = bind_password.getText().toString().trim();
				if (checkUserName() && checkPassword()) {
					Log.i("login", "username" + username + "password" + password);
					bindSina(username, password);
				}
			}
			break;
		case R.id.tittle_common_register:
			Intent intent=new Intent(BindActivity.this,RegisterActivity.class);
			startActivityForResult(intent, 0);
			finish();
			break;
		default:
			break;
		}
	}
	private void bindSina(String username2, String password2) {
		RequestParams params = new RequestParams();
		params.put("sina_uid", ConstantsValue.sina.getString("uid"));
		params.put("token", ConstantsValue.sina.getString("access_token"));
		params.put("oauth2_expiretime", ConstantsValue.sina.getString("expires_in")+"");
		params.put("username", username2);
		params.put("password", password2);
		final ProgressHUD ProgressDialog = ProgressHUD.show(this, "正在加载...", true, true, null);
		AddClient.get(Address.BINDSINA, params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, String content) {
				if (null != ProgressDialog && ProgressDialog.isShowing()) {
					ProgressDialog.dismiss();
				}
				UserInfo userinfo = JSON.parseObject(content, UserInfo.class);
				if (userinfo.getResult().equals("0")) {
					ConstantsValue.uid = userinfo.getUserinfo().uid;
					ConstantsValue.setUser(userinfo.getUserinfo());
					ConstantsValue.isLogin = true;
					Editor edit = sp.edit();
					edit.putString("username", userinfo.userinfo.username);
					edit.putString("password", "");
					edit.commit();
					handler.sendEmptyMessage(LOGIN_OK);
					Toast.makeText(getApplicationContext(),"绑定微博成功", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(getApplicationContext(), userinfo.getMsg(), Toast.LENGTH_SHORT).show();
				}
				super.onSuccess(statusCode, content);
			}

			@Override
			public void onFailure(Throwable error, String content) {
				if (null != ProgressDialog && ProgressDialog.isShowing()) {
					ProgressDialog.dismiss();
				}
				super.onFailure(error, content);
			}
		});
	}
	private void bindQQ(String username2, String password2) {
		RequestParams params = new RequestParams();
		params.put("username", username2);
		params.put("password", password2);
		params.put("conuin", "");
		params.put("conuinsecret", "");
		params.put("conopenid", ConstantsValue.info.openid);
		final ProgressHUD ProgressDialog = ProgressHUD.show(this, "正在加载...", true, true, null);
		AddClient.get(Address.BINDQQ, params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, String content) {
				if (null != ProgressDialog && ProgressDialog.isShowing()) {
					ProgressDialog.dismiss();
				}
				UserInfo userinfo = JSON.parseObject(content, UserInfo.class);
				if (userinfo.getResult().equals("0")) {
					ConstantsValue.uid = userinfo.getUserinfo().uid;
					ConstantsValue.setUser(userinfo.getUserinfo());
					ConstantsValue.isLogin = true;
					Editor edit = sp.edit();
					edit.putString("username", userinfo.userinfo.username);
					edit.putString("password", "");
					edit.commit();
					handler.sendEmptyMessage(LOGIN_OK);
					Toast.makeText(getApplicationContext(),"绑定成功", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(getApplicationContext(), userinfo.getMsg(), Toast.LENGTH_SHORT).show();
				}
				super.onSuccess(statusCode, content);
			}

			@Override
			public void onFailure(Throwable error, String content) {
				if (null != ProgressDialog && ProgressDialog.isShowing()) {
					ProgressDialog.dismiss();
				}
				super.onFailure(error, content);
			}
		});
	}

	private boolean checkPassword() {
		if (TextUtils.isEmpty(password)) {
			Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	private boolean checkUserName() {
		if (TextUtils.isEmpty(username)) {
			Toast.makeText(getApplicationContext(), "用户名不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
}
